package com.gohi.main.user.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.commons.validator.Msg;
import org.apache.log4j.Logger;
import org.hibernate.TransactionException;

import com.gohi.core.web.action.BaseAction;
import com.gohi.main.system.hbm.ShopgooArea;
import com.gohi.main.system.service.ISystemService;
import com.gohi.main.user.hbm.ShopgooMember;
import com.gohi.main.user.hbm.ShopgooMessage;
import com.gohi.main.user.hbm.ShopgooWxMember;
import com.gohi.main.user.service.IShopgooWxMemberService;
import com.gohi.main.user.service.IUserService;
import com.gohi.main.util.AppJsonUtils;
import com.gohi.main.util.MD5;
import com.gohi.main.util.MsgUtils;
import com.gohi.main.util.StringUtil;
import com.gohi.main.util.Utils;

import com.gohi.main.wxapi.util.AccoutUtil;
import com.gohi.main.wxapi.vo.JsApi;
import com.gohi.main.wxapi.vo.WxAccount;

/**
 * 用户信息   ACTION
 * 
 * @author 用户信息
 * 
 */
public class UserAction extends BaseAction {
	Logger logger = Logger.getLogger(UserAction.class);
	private ISystemService systemService;
	// 用户
	private IUserService userService;
	private String user;
	private String pswd;
	private IShopgooWxMemberService shopgooWxMemberService;
	public String alert(){
		String flag=this.getRequest().getParameter("flag");
		if(flag.equals("success")){
			this.alert="操作成功！";
			this.url="user.do";
			return "SUCCESS"; 
		}else{
			this.alert="操作失败！";
			this.url="user.do";
			return "ERROR"; 
		}
				
	}
	//首页
	public String index() {
		HttpSession session = this.getRequest().getSession();
		//判断登录否
		if(session.getAttribute("user")==null){
			return "login";
		}
		return "index";
	}
	
	
	public String tofindpwd(){
		
		return "findpwd";
	}
	//充值密码
	public void torepwd() throws IOException{
		String phone=this.getRequest().getParameter("phone");
		//1验证成功  2验证码过期 或者不存在  
		String code=this.getRequest().getParameter("yzm");
		if(!Utils.show(phone).equals("")){
			ShopgooMessage msg = userService.queryMessage(phone);
			//验证码有效期5分钟
			if(msg!=null && StringUtil.getTimeSimple()-msg.getTime()<420 && code.equals(msg.getCode())){
				ShopgooMember user =userService.getByUserName(phone);
				if(user==null){
					this.getResponse().getWriter().print("2");//注册失败
				}else{
					//直接更新密码 
					//System.err.println(pswd);
					userService.updateKey("password", MD5.MD5Encode(pswd),user.getMember_id() + "");
					this.getResponse().getWriter().print("1");
				}
			}else{
				//验证码过期
				this.getResponse().getWriter().print("2");
			}
			
		}else{
			//参数不合法
			this.getResponse().getWriter().print("2");
		}		
	}
	//用户注册
	public String reg(){
		
		return "user_reg";
	}
	//个人中心
	public String member(){
		
		return "index";
	}
	//用户登录
	public void login() throws IOException{
		if(!Utils.show(user).equals("") && !Utils.show(pswd).equals("")){
			ShopgooMember u = userService.Login(user, pswd);
			if(u==null){
				//登录失败
				this.getResponse().getWriter().print("0");
			}else{

				 this.getRequest().getSession().setAttribute("user", u);
				 //给他绑定微信号
				 if(this.getSession().getAttribute("openid")!=null){
				 //先判断是否绑定了
			     if(shopgooWxMemberService.wxIsBind(u.getMember_id()+"")==0){
				 ShopgooWxMember wxuser = shopgooWxMemberService.getById(this.getSession().getAttribute("openid").toString());
				 if(wxuser!=null){
						//绑定微信号
						wxuser.setRealid(u.getMember_id()+"");
						shopgooWxMemberService.saveOrUpdate(wxuser);
					}
				 }
			}/*else{
				System.err.println("已经绑定了！");
			}*/
				//登录成功
				this.getResponse().getWriter().print("1");
			}
			
		}else{
			this.getResponse().getWriter().print("0");
		}
	}
	
	//用户注册
	public void doreg() throws IOException{
		ShopgooMember m =new ShopgooMember();
		HttpSession session = this.getRequest().getSession();
		String phone=this.getRequest().getParameter("phone");
		String time =this.getRequest().getParameter("time");
		//1参数错误 0验证成功  2验证码过期 或者不存在  

		String code=this.getRequest().getParameter("yzm");
		if(!Utils.show(phone).equals("")){
			ShopgooMessage msg = userService.queryMessage(phone);
			//验证码有效期5分钟
			if(msg!=null && StringUtil.getTimeSimple()-msg.getTime()<420 && code.equals(msg.getCode())){
				if(userService.getByNames(user)==0){
					m.setMember_name(phone);
					m.setPassword(pswd);
					m.setEmail(" ");
					m.setMobile(phone);
					String openid= this.getSession().getAttribute("openid")+"";
					ShopgooWxMember wxuser = shopgooWxMemberService.getById(openid);
					if(wxuser==null){
						m.setNickname("null");
						m.setAvatar("./image/employee.png");
					}else{
						m.setNickname(phone);
						m.setAvatar(wxuser.getHeadimg());
					}
					
					m.setMember_degree("0");
					m.setMember_point(0);
					m.setMember_contribution(0);
					m.setProvince("0");
					m.setRegister_time(StringUtil.getTimeSimple());
					m.setCity("0");
					m.setDistrict("0");
					int sum = userService.regMember(m);
					if(sum==1){
						m=userService.Login(phone, pswd);//重新查询一下;
						if(wxuser!=null){
							//绑定微信号
							wxuser.setRealid(m.getMember_id()+"");
							shopgooWxMemberService.saveOrUpdate(wxuser);
						}
						
						 this.getRequest().getSession().setAttribute("user", m);
						this.getResponse().getWriter().print("1");
						
					}
					
				}else{
					//用户已经被注册
					this.getResponse().getWriter().print("2");//注册失败
				} 
				
		
			}else{
				//验证码过期
				this.getResponse().getWriter().print("2");
			}
			
		}else{
			//参数不合法
			this.getResponse().getWriter().print("0");
		}		
		
	}
	//验证用户名
	public void checkusername() throws TransactionException, IOException{
		this.getResponse().getOutputStream().print(userService.getByNames(this.getRequest().getParameter("username")));
	}
	
	//发送短信的接口
	public void sendmsg() throws IOException{
		String phone=this.getRequest().getParameter("phone");
		String time =this.getRequest().getParameter("time");
		String type  = this.getRequest().getParameter("type");//定义的短信模板
		String method= getRequest().getParameter("method");
		if(Utils.show(type).equals("")){
			type="1";
		}
		if(!Utils.show(phone).equals("") && time!=null && MsgUtils.isMobile(phone)==true){
			//判断发送时间
			  ShopgooMessage msg = userService.queryMessage(phone);
	
					if(msg!=null){
						if(StringUtil.getTimeSimple()-msg.getTime()<60){
							//发送时间太短了
							this.getResponse().getWriter().print("2");
							return ;
						}
					}
					//发送时间超过60秒了
					//再查询手机号码是否已经注册
					int count =userService.checkPhoneReg(phone);
					if(count>0){
						//表示注册的
						if(!Utils.show(method).equals("findpwd")){
							//手机号码已经被注册 
							this.getResponse().getWriter().print("2");
							return ;
						}
						
					}
					
					//调用短信发送接
					String code =StringUtil.fillString(new Random().nextInt(10000)+"", "0", 4, StringUtil.FILL_TYPE_LEFT);
					if(type.equals("1")){
						//发送注册的验证
						MsgUtils.SendMsg(phone, MsgUtils.getRegCode(code));
					}else if(type.equals("2")){
						//发送找回密码的验证
						if(count!=0){
							MsgUtils.SendMsg(phone, MsgUtils.getFindPwdCode(code));
						}else{
							this.getResponse().getWriter().print("2");
							return ;
						}
						
					}
					msg=new ShopgooMessage();
					msg.setMobile(phone);
					msg.setCode(code);
					msg.setTime(StringUtil.getTimeSimple());
					msg.setType(Integer.parseInt(type));
					userService.addMessage(msg);
					this.getResponse().getWriter().print("0");
			
	
		}else{
			//发送失败
			this.getResponse().getWriter().print("1");
		}
	}
	
	//检测验证码
	public void checkMsgCode() throws IOException{
		//1参数错误 0验证成功  2验证码过期 或者不存在  
		String phone=this.getRequest().getParameter("phone");
		String code=this.getRequest().getParameter("yzm");
		if(!Utils.show(phone).equals("")){
			ShopgooMessage msg = userService.queryMessage(phone);
			//验证码有效期5分钟
			if(msg!=null && StringUtil.getTimeSimple()-msg.getTime()<420 && code.equals(msg.getCode())){
				
				this.getResponse().getWriter().print("0");
			}else{
				this.getResponse().getWriter().print("2");
			}
			
		}else{
			this.getResponse().getWriter().print("1");
		}
	}
	
	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPswd() {
		return pswd;
	}

	public void setPswd(String pswd) {
		this.pswd = pswd;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IShopgooWxMemberService getShopgooWxMemberService() {
		return shopgooWxMemberService;
	}

	public void setShopgooWxMemberService(
			IShopgooWxMemberService shopgooWxMemberService) {
		this.shopgooWxMemberService = shopgooWxMemberService;
	}
	
}
